<?php
namespace Home\Controller;
use Think\Controller;
use Think\Page;
class IndexController extends Controller {
    public function index(){
        $r=M('student');//M函数用于实例化一个没有模型文件的Model
        $a=M('liuyan');
        $liuyan = $a->select();
        $this->assign('liuyan',$liuyan);


        //$s=$r->field('name,max(age)')->group('class')->having('count(id)>2')->select(); 
        //dump($s);

        //留下需要用到的条件
        foreach(I('get.') as $k=>$v){
            if($v != ''){
                $where[$k] = $v;
            }          
        }
        //dump(I('get.'));
        if($where['name']==''){
            unset($where['name']);
        }else{
            $where['name']=array('like','%'.$where['name'].'%');
        }
        if($where['sex']==''){
            unset($where['sex']);
        }
        if($where['class']==''){
            unset($where['class']);
        }
        if($where['min']==''){
            $min=0;
        }else{
            $min=$where['min'];
        }
        if($where['max']==''){
            $max=200;
        }else{
            $max=$where['max'];
        }
        $where['age']=array('between',[$min,$max]);

        if($where){
            $totalRows= $r->where($where)->count();// 查询满足要求的总记录数
        }else{
            $totalRows=$r->count();
        }
        //dump()
        //分页
        $listRows=3;
        $Page=new Page($totalRows,$listRows);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $show=$Page->show();
        
        $p=$_GET['p'];
        $res=$r->where($where)->limit($p,$listRows)->select();
        $this->assign('res',$res);
         if(IS_AJAX){
            if($res!=null){
                $data['data']=$this->fetch('htt');
                $data['errCode']=1;
                $data['errMsg']='成功';
                
            }else{
                $data['errCode']=0;
                $data['errMsg']='无数据';
                $data['data']=null;
            }

            $this->ajaxReturn($data);
        }

        $this->assign('show',$show);
        
        $this->display();
    }
    public function del(){
        $r=M('student');
        $id = I('get.id');
        //dump($id);die;
        $m = $r->where(array('id'=>$id))->delete();
        if($m){
            $this->success("删除成功");//success跳转
        }else{
            $this->error("删除失败");
        }
        
    }
    public function add(){
        $r=M('student');
        $data['name'] = I('name');
        $data['sex'] = I('sex');
        $data['age'] = I('age');
        $data['class'] = I('class');
        $m = $r->add($data);
        if($m){
            $this->success("添加成功");
        }       
    }
    public function update(){
        $r=M('student');
        $id = I('id');
        $res = $r->where("id=$id")->select();
        $this->assign('res',$res);
        $this->display();

    }
    public function doUpdate(){
        $r=M('student');
        $data['id'] = I('post.id');
        $data['name'] = I('post.name');
        $data['sex'] = I('post.sex');
        $data['age'] = I('post.age');
        $data['class'] = I('post.class');
        $id=$data['id'];
        $m=$r->where("id=$id")->save($data);
        if($m){
            $this->success("修改成功","Index/index.html");
        }
    }
    //留言删除
    public function del2(){
        $r=M('liuyan');
        $id = I('get.id');
        dump($id);
        $m = $r->where(array('id'=>$id))->delete();
        if($m){
            $this->success("删除成功");//success跳转
        }else{
            $this->error("删除失败");
        }       
    }
}